package org.weishe.view.lock;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;


import java.util.ArrayList;
import java.util.List;

/**
 * 图形密码控件
 */
public class LockView extends View {

    private int mWidth;
    private int mHeight;

    private float mTouchX;//当前触控的坐标位置
    private float mTouchY;
    /**
     * 是否还在圆圈里面
     */
    private boolean inCircleViewFlag = false;

    private boolean finishFlag = false;

    private Paint mBackGroundPaint, mBCirclePaint, mMCirclePaint, mLinePaint;
    private float bigCircleRadius, smallCircleRadius;

    private int mCircleColor;
    private int mLineColor;

    private float mLineWidth = 10;


    private OnFinishInputListener onFinishInputListener;
    /**
     * 密码
     */
    private List<Integer> pwd = new ArrayList<Integer>();


    //九个按钮
    private List<CircleView> mCircleViews = new ArrayList<CircleView>();


    public LockView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public LockView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public LockView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LockView);
        mLineColor = a.getColor(R.styleable.LockView_lineColor, getResources().getColor(android.R.color.white));
        mCircleColor = a.getColor(R.styleable.LockView_circleColor, getResources().getColor(android.R.color.holo_blue_light));

        //   mHeight=640;
        // Set up a default TextPaint object
        mBackGroundPaint = new Paint();

        mBackGroundPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mBCirclePaint = new Paint();
        mBCirclePaint.setFlags(Paint.ANTI_ALIAS_FLAG);

        mBCirclePaint.setColor(mLineColor);

        mMCirclePaint = new Paint();
        mMCirclePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mMCirclePaint.setColor(mLineColor);

        mLinePaint = new Paint();
        mLinePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mLinePaint.setColor(mCircleColor);
        mLinePaint.setStrokeWidth(mLineWidth);
    }


    /**
     * 填充图形
     */
    private void fillCircle() {
        float bigCircleRadius = getCircleRadius();
        float smallCircleRadius = bigCircleRadius - (bigCircleRadius / 4);
        CircleView v1 = new CircleView(bigCircleRadius * 3, mHeight / 2 - bigCircleRadius * 4, 1);
        CircleView v2 = new CircleView(bigCircleRadius * 7, mHeight / 2 - bigCircleRadius * 4, 2);
        CircleView v3 = new CircleView(bigCircleRadius * 11, mHeight / 2 - bigCircleRadius * 4, 3);
        CircleView v4 = new CircleView(bigCircleRadius * 3, mHeight / 2, 4);
        CircleView v5 = new CircleView(bigCircleRadius * 7, mHeight / 2, 5);
        CircleView v6 = new CircleView(bigCircleRadius * 11, mHeight / 2, 6);
        CircleView v7 = new CircleView(bigCircleRadius * 3, mHeight / 2 + bigCircleRadius * 4, 7);
        CircleView v8 = new CircleView(bigCircleRadius * 7, mHeight / 2 + bigCircleRadius * 4, 8);
        CircleView v9 = new CircleView(bigCircleRadius * 11, mHeight / 2 + bigCircleRadius * 4, 9);
        this.mCircleViews.add(v1);
        this.mCircleViews.add(v2);
        this.mCircleViews.add(v3);
        this.mCircleViews.add(v4);
        this.mCircleViews.add(v5);
        this.mCircleViews.add(v6);
        this.mCircleViews.add(v7);
        this.mCircleViews.add(v8);

        this.mCircleViews.add(v9);
    }

    private void drawCircle(Canvas canvas, CircleView circleView) {
        if (circleView.isSelected()) {
            mBCirclePaint.setColor(mLineColor);
            mMCirclePaint.setColor(mCircleColor);
        } else {
            mBCirclePaint.setColor(mCircleColor);
            mMCirclePaint.setColor(mLineColor);
        }
        canvas.drawCircle(circleView.getX(), circleView.getY(), bigCircleRadius, mBCirclePaint);
        canvas.drawCircle(circleView.getX(), circleView.getY(), smallCircleRadius, mMCirclePaint);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        this.mHeight = this.getMeasuredHeight();
        this.mWidth = this.getMeasuredWidth();
        bigCircleRadius = getCircleRadius();
        smallCircleRadius = bigCircleRadius - (bigCircleRadius / 4);


        //画圈圈
        if (mCircleViews.isEmpty()) {
            this.fillCircle();
        }
        for (CircleView c : mCircleViews) {
            drawCircle(canvas, c);
        }
        //画直线
        int length = pwd.size();
        for (int i = 0; i < length; i++) {
            if (i < length - 1) {
                CircleView c1 = mCircleViews.get(pwd.get(i) - 1);
                CircleView c2 = mCircleViews.get(pwd.get(i + 1) - 1);

                canvas.drawLine(c1.getX(), c1.getY(), c2.getX(), c2.getY(), mLinePaint);

            } else if ((i == length - 1) && !finishFlag) {
                CircleView c1 = mCircleViews.get(pwd.get(i) - 1);
                canvas.drawLine(c1.getX(), c1.getY(), mTouchX, mTouchY, mLinePaint);
            }
        }

    }


    private float getCircleRadius() {

        float radius = 0;

        radius = mWidth / 7 / 2;

        return radius;
    }

    /**
     * 当在在移动时判断
     *
     * @param x
     * @param y
     */
    private void touchAndMove(float x, float y) {
        float bigCircleRadius = getCircleRadius();
        int count = 0;
        //判断9个圆圈是否被选中
        Log.v("org.weishe.weichat.touch", "begin inCircleViewFlag:" + inCircleViewFlag);
        for (CircleView c : mCircleViews) {
            if ((c.getX() - bigCircleRadius) < x && x < (c.getX() + bigCircleRadius) && (c.getY() - bigCircleRadius) < y && y < (c.getY() + bigCircleRadius)) {
                if (!inCircleViewFlag) {
                    if (!c.isSelected()) {
                        c.setSelected(true);
                        pwd.add(c.getNumber());
                    }
                    inCircleViewFlag = true;
                }
                break;
            } else {
                count++;
            }
        }
        if (count == 9) {
            inCircleViewFlag = false;
        }
        Log.v("org.weishe.weichat.touch", "end inCircleViewFlag:" + inCircleViewFlag);
        invalidate();
    }


    private void clearView() {
        inCircleViewFlag = false;
        //清除
        pwd.clear();
        //重绘界面
        for (CircleView c : mCircleViews) {
            c.setSelected(false);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mTouchX = event.getX();
        mTouchY = event.getY();
        /**
         * 该事件是否响应完成
         */
        boolean flag = true;
        touchAndMove(mTouchX, mTouchY);

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //清理界面
                clearView();
                finishFlag = false;
                Log.v("org.weishe.weichat.touch", "action:ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.v("org.weishe.weichat.touch", "action:ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.v("org.weishe.weichat.touch", "action:ACTION_UP");

                Log.v("org.weishe.weichat.touch", "pwd:" + pwd);
                //返回输入的密码
                if (onFinishInputListener != null) {
                    onFinishInputListener.onFinish(pwd);
                }
                flag = false;
                finishFlag = true;
                break;
        }
        return flag;
    }


    public interface OnFinishInputListener {
        public void onFinish(List<Integer> password);
    }

    public void setOnFinishInputListener(OnFinishInputListener onFinishInputListener) {
        this.onFinishInputListener = onFinishInputListener;
    }

    public int getLineColor() {
        return mLineColor;
    }

    public void setLineColor(int lineColor) {
        this.mLineColor = lineColor;
    }

    public float getLineWidth() {
        return mLineWidth;
    }

    public void setLineWidth(float lineWidth) {
        this.mLineWidth = lineWidth;
    }

    public int getCircleColor() {
        return mCircleColor;
    }

    public void setCircleColor(int circleColor) {
        this.mCircleColor = circleColor;
    }
}
